package ch.fhnw.ams.sudoku.gui;

import static org.testng.Assert.assertEquals;
import java.io.File;
import java.net.URL;

import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JFileChooserOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JMenuItemOperator;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import ch.fhnw.ams.sudoku.Globals;
import ch.fhnw.ams.sudoku.SudokuGame;
import ch.fhnw.ams.sudoku.SudokuGrid;
import ch.fhnw.ams.sudoku.io.UserProperties;

public class T3_SolveAutomatically {
	
	private static File testFile;
	private static File testDir;

	@BeforeClass
	public void beforeClass() throws Exception {
		File userPropFile = new File(UserProperties.USER_PROPERTIES_PATH);
		userPropFile.delete();
		URL testDirURL = ClassLoader.getSystemResource("testdata-puzzle");
		testDir = new File(testDirURL.toURI());
		testFile = new File(testDir.getAbsolutePath() + Globals.SEP + "hard_001.ssk");
	}
	
	@Test
	public void solveAutomatically() throws Exception {
		SudokuGame sudokuGame = new SudokuGame();
		new SudokuFrame(sudokuGame);
		
		//open Sudoku
		JFrameOperator frameOp = new JFrameOperator();
		new JMenuItemOperator(frameOp, "File").clickMouse();
		new JMenuItemOperator(frameOp, "Open").pushNoBlock();
		
		JFileChooserOperator fileChooserOp = new JFileChooserOperator(frameOp);
		fileChooserOp.setCurrentDirectory(testDir);
		fileChooserOp.setSelectedFile(testFile);
		fileChooserOp.approve();
		
		//Solve and check Sudoku
		new JButtonOperator(frameOp, "Solve").push();
		
		Thread.sleep(1000);
		
		SudokuGrid grid = sudokuGame.getSudokuGrid();
		assertEquals(grid.getField(0, 0).getNumber(), 5);
		assertEquals(grid.getField(8, 8).getNumber(), 4);
		assertEquals(grid.getField(3, 3).getNumber(), 7);
		
		frameOp.dispose();
	}
	
}
